package trystero.lib.auth;

import java.io.InputStream;
import java.util.Vector;

import trystero.lib.http.HTTPClient;
import trystero.lib.http.HTTPRequestState;
import trystero.lib.http.POSTData;
import trystero.lib.http.hc.HTTPClientImpl;
import trystero.lib.util.StreamUtils;

public abstract class ReaderAuth
{
   protected String username;
   protected String password;
   HTTPClient client;

   public ReaderAuth(String username, String password)
   {
      this.username = username;
      this.password = password;
   }

   public HTTPClient client() { return this.client; }
   public void client(HTTPClient client) { this.client = client; }

   public abstract void authenticate(HTTPRequestState state) throws Exception;

   public void stampToken(HTTPRequestState state) throws Exception
   {
      if(state.data() == null)
	 return; 

      POSTData data = state.data();
      data.add("T", token());
      state.data(data);
   }

   public String token() throws Exception
   {
      if(client() == null)
         client(new HTTPClientImpl());

      HTTPRequestState state = new HTTPRequestState();
      state.url("http://www.google.com/reader/api/0/token");
      authenticate(state);

      InputStream input = client.get(state);

      return StreamUtils.slurp(input);
   }
}
